﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Aliphant.Common;
using Aliphant.Mvvm.XEvent.Ioc;
using Aliphant.UI;
using Aliphant.UI.Forms.Notify;
using Aliphant.UI.Windows;

using TestModule.Widgets;

namespace TestModule.Views
{
    /// <summary>
    /// XWidgetPage.xaml 的交互逻辑
    /// </summary>
    public partial class XWidgetPage : UserControl, IPage
    {
        private Dictionary<string, Widget> widgets = new Dictionary<string, Widget>();

        public XWidgetPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 时钟小组件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWidgetClock(object sender, RoutedEventArgs e)
        {
            if (CanShowWidget("clock"))
            {
                return;
            }

            WidgetClock clock = new WidgetClock();
            Widget widget = new Widget(clock, false, false);
            widget.Show();
            this.widgets.Add("clock", widget);
        }

        /// <summary>
        /// 浏览器小组件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWidgetBrowser(object sender, RoutedEventArgs e)
        {
            if (CanShowWidget("browser"))
            {
                return;
            }

            //Demo demo = new Demo();
            //demo.Show();

            WidgetBrowser browser = new WidgetBrowser("https://www.bing.com");
            Widget widget = new Widget(browser, true, true);
            widget.Show();
            this.widgets.Add("browser", widget);
        }

        /// <summary>
        /// 盯盘小组件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWidgetStock(object sender, RoutedEventArgs e)
        {
            WidgetStock stock = new WidgetStock();
            Widget widget = new Widget(stock, true, false);
            widget.Show();
            this.widgets.Add(Guid.NewGuid().ToString(), widget);
        }

        /// <summary>
        /// 辅助小工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTSModule(object sender, RoutedEventArgs e)
        {
            if (CanShowWidget("ts"))
            {
                return;
            }
            try
            {
                var page = (IWidget)UIContainerLocator.Container.Resolve("tsWidget");
                Widget widget = new Widget(page);
                widget.Show();
                this.widgets.Add("ts", widget);
            }
            catch (InvalidOperationException)
            {
                NotifyIconHelper.ShowBalloon("tsWidget模块未注册", "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                NotifyIconHelper.ShowBalloon(ex.Message, "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
        }

        /// <summary>
        /// 日历
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCalenderModule(object sender, RoutedEventArgs e)
        {
            if (CanShowWidget("calender"))
            {
                return;
            }
            try
            {
                var page = (IWidget)UIContainerLocator.Container.ResolveNew("calender");
                Widget widget = new Widget(page, false, false);
                widget.Show();
                this.widgets.Add("calender", widget);
            }
            catch (InvalidOperationException)
            {
                NotifyIconHelper.ShowBalloon("calender模块未注册", "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                NotifyIconHelper.ShowBalloon(ex.Message, "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
        }

        /// <summary>
        /// 脚本执行器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnJavaScript(object sender, RoutedEventArgs e)
        {
            if (CanShowWidget("js"))
            {
                return;
            }
            try
            {
                var page = (IWidget)UIContainerLocator.Container.ResolveNew("script");
                Widget widget = new Widget(page, false, false);
                widget.Show();
                this.widgets.Add("js", widget);
            }
            catch (InvalidOperationException)
            {
                NotifyIconHelper.ShowBalloon("script模块未注册", "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                NotifyIconHelper.ShowBalloon(ex.Message, "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
        }

        /// <summary>
        /// 脚本组件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnJavaScriptWidget(object sender, RoutedEventArgs e)
        {
            if (CanShowWidget("jswidget"))
            {
                return;
            }
            try
            {
                var page = (IWidget)UIContainerLocator.Container.ResolveNew("widgetScript");

                Widget widget = new Widget(page, "F:\\Desktop\\中转\\Scripts\\b.js");
                widget.Show();
                this.widgets.Add("jswidget", widget);
            }
            catch (InvalidOperationException)
            {
                NotifyIconHelper.ShowBalloon("script模块未注册", "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                NotifyIconHelper.ShowBalloon(ex.Message, "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
        }

        /// <summary>
        /// 注册机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnRegister(object sender, RoutedEventArgs e)
        {
            if (CanShowWidget("tsres"))
            {
                return;
            }
            try
            {
                var page = (IWidget)UIContainerLocator.Container.ResolveNew("tsRegister");
                Widget widget = new Widget(page, true, false);
                widget.Show();
                this.widgets.Add("tsres", widget);
            }
            catch (InvalidOperationException)
            {
                NotifyIconHelper.ShowBalloon("script模块未注册", "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                NotifyIconHelper.ShowBalloon(ex.Message, "XWidget", 0, NotifyIconHelper.NotifyLevel.Error);
            }
        }

        private bool CanShowWidget(string key)
        {
            if (this.widgets.ContainsKey(key))
            {
                var wb = this.widgets[key];
                if (wb != null && !wb.IsClosed)
                {
                    wb.Show();
                    return true;
                }
                this.widgets.Remove(key);
            }
            return false;
        }

        public void Close()
        {
            foreach (var widget in this.widgets.Values)
            {
                widget.Close();
            }
        }


    }
}
